package com.fw.application.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fw.application.controller.base.BaseController;
import com.fw.common.Builder;
import com.fw.common.IdXD;
import com.fw.mes.Result;
import com.fw.system.web.model.entity.TbCollection;
import com.fw.system.web.model.entity.TbGuns;
import com.fw.system.web.service.ITbCollectionService;
import com.fw.system.web.service.ITbGunsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

import static com.fw.mes.ResultUtils.success;

/**
 * <p>
 * 收藏表 前端控制器
 * </p>
 *
 * @author
 * @since 2021-12-04
 */
@RestController
@RequestMapping("/collection")
@Api(tags = "收藏相关")
@CrossOrigin
public class TbCollectionController implements BaseController {

    @Autowired
    private ITbCollectionService collectionService;
    @Autowired
    private ITbGunsService gunsService;
    @Autowired
    private IdXD idXD;

    @GetMapping("/addCollet")
    @ApiOperation("添加收藏 回传id 如果这条数据被收藏过 就执行取消收藏操作 isType 0取消 1收藏")
    public Result addColletByGunsId(@RequestParam String id) {

        String userId = this.getUser().getId();
        TbCollection collection = collectionService.getOne(Wrappers.<TbCollection>lambdaQuery().eq(TbCollection::getUserId, userId).eq(TbCollection::getGunsId, id));
        TbGuns guns = gunsService.getById(id);
        if (collection == null) {
            Builder.of(TbCollection::new)
                    .with(TbCollection::setId, idXD.nextId())
                    .with(TbCollection::setUserId, userId)
                    .with(TbCollection::setAmount, guns.getAmount())
                    .with(TbCollection::setGunsId, guns.getId())
                    .with(TbCollection::setGunsName, guns.getGunsName())
                    .with(TbCollection::setImgUrl, guns.getImgUrl())
                    .with(TbCollection::setCreateTime, LocalDateTime.now())
                    .with(TbCollection::setIsType, 1).build().insert();
        } else {
            if (collection.getIsType().equals(0)){
                collection.setIsType(1);
                collectionService.updateById(collection);
            }else {
                collection.setIsType(0);
                collectionService.updateById(collection);
            }
        }
        return success();
    }

    @GetMapping("/getCollectionDetail")
    @ApiOperation("我的收藏查看详情")
    public Result getCollectionDetail(@RequestParam  String id) {

        String userId = this.getUser().getId();

        TbCollection collection = collectionService.getOne(Wrappers.<TbCollection>lambdaQuery().eq(TbCollection::getUserId, userId).eq(TbCollection::getGunsId, id));

        return success(collection);
    }


    @GetMapping("/getCollection")
    @ApiOperation("查询我的收藏列表")
    public Result<PageInfo<TbCollection>> getCollection(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {

        String userId = this.getUser().getId();
        PageHelper.startPage(pageNum,pageSize);
        List<TbCollection> list = collectionService.list(Wrappers.<TbCollection>lambdaQuery()
                .eq(TbCollection::getIsType,1).eq(TbCollection::getUserId, userId).orderByDesc(TbCollection::getCreateTime));
        PageInfo<TbCollection> info = new PageInfo<>(list);
        return success(info);
    }

}

